/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.util;

import com.ddd.context.Parameters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Util {

    public static void checkBEAuthorized(HttpServletRequest request, HttpServletResponse response) {
        try {
            Parameters para = new Parameters(request);
            if (!para.isBEAuthorized()) {
                response.sendRedirect("index.jsp");
                return;
            }
        } catch (Exception e) {
            return;
        }
    }

    public static void checkFEAuthorized(HttpServletRequest request, HttpServletResponse response) {
        try {
            Parameters para = new Parameters(request);
            if (!para.isFEAuthorized()) {
                response.sendRedirect("index.jsp");
                return;
            }
        } catch (Exception e) {
            return;
        }
    }

    public static String getTodayWithTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }

    public static String getToday() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(new Date());
    }

    public static String getThisYear() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        return df.format(new Date());
    }

    public static String formatSerialNo(int numberOfZero, int serNo) {
        if (numberOfZero <= 0 || serNo < 0) {
            return null;
        }

        String noString = String.valueOf(serNo);

        int len = numberOfZero - noString.length();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < len; i++) {
            sb.append("0");
        }

        sb.append(noString);
        return sb.toString();
    }

    public static void println(Object obj) {
        System.out.println(obj);
    }

    public static void print(Object obj) {
        System.out.println(obj);
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static String trimStringToFixedLength(String str, int len) {
        String rval = null;

        if (str == null) {
            return null;
        }

        if (str.length() < len) {
            rval = new String(str) + "...";
        } else {
            rval = str.substring(0, len) + "...";
        }

        return rval;
    }

    public static String replaceBRToNewline(String str) {
        return str.replaceAll("<br>", "\n");
    }

    public static String getFilenameWithoutExtention(String file) {
        File f = new File(file);
        String filename = f.getName();
        return filename.substring(0, filename.lastIndexOf('.'));
    }

    public static BufferedReader getBufferedReader(String filename, String encoding) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding), 32768);
            return br;
        } catch (Exception ex) {
            return null;
        }
    }
    
    public static String formatAmount (Double d){
        NumberFormat format = new DecimalFormat("#0");
        return format.format(d);
        
    }
}
